<?php
namespace app\admin\controller;
use think\Controller;
use think\Request;
use think\Session;
use think\Db;
class Banner extends Common
{

    public function index()
    {
       return $this -> fetch();
    }
    public function b_list(){
        $data = Db::table('banner') ->paginate(10);
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function b_add(){
        $data = Request::instance() -> Param();
        $res = '';
        if(!empty($data)){
            $res = Db::table('banner')-> where($data) -> find();

        }
        $this->assign('res',$res);
        return $this->fetch();
    }
    public function dob_add(){
        $param = Request::instance() -> Param();
        $param['b_time'] = time();
        $str = is_base64($param['b_img']);
        if($str){
            $image = base64_img($param['b_img']);
            if($image['error']){
                $img = $image['msg'];
            }else{
                return(array(
                    'error' => 1,
                    'msg'   => $image['msg']
                ));
            }
            $data['b_img'] = $img;
            $param['b_img'] = $img;
            $old_img = Db::table('banner') -> where('b_id',$param['b_id']) -> find();
            if($old_img['b_img']){
                unlink(ROOT_PATH  . 'public'.$old_img['b_img']);
            }
        }

        $data = $param;
        if(!$data['b_id']){
            unset($data['b_id']);
        }else{
            $result = Db::table('banner') -> where('b_id',$param['b_id'])-> update($param);
            if($result){
                return(array(
                    'error' => 0,
                    'msg'   => '编辑成功',
                    'url'   => '/admin/Banner/b_list'
                ));
            } else {
                return(array(
                    'error' => 1,
                    'msg'   => '编辑失败',
                    'url'   => '/admin/Banner/b_add'
                ));
            }
        }

        $result = Db::table('banner') -> insert($data);
            if($result){
                return(array(
                    'error' => 0,
                    'msg'   => '添加成功',
                    'url'   => '/admin/Banner/b_list'
                ));
            } else {
                return(array(
                    'error' => 1,
                    'msg'   => '添加失败',
                    'url'   => '/admin/Banner/b_add'
                ));

        }
    }
    /*
     * 删除banner
     * */
    public function b_delete(){
        $data = Request::instance() -> Param();
        $result = Db::table('banner') -> where($data) -> delete();
        if($result){
            return(array(
                'error' => 0,
                'msg'   => '删除成功',
                'url'   => '/admin/Banner/b_list'
            ));
        } else {
            return(array(
                'error' => 1,
                'msg'   => '删除失败',
                'url'   => '/admin/Banner/b_list'
            ));
        }
    }
}
